package com.itheima.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.common.CustomException;
import com.itheima.entity.Dish;
import com.itheima.dto.DishDto;
import com.itheima.entity.DishFlavor;
import com.itheima.mapper.DishDao;
import com.itheima.service.DishFlavorService;
import com.itheima.service.DishService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Slf4j
@Transactional
@Service
public class DishServiceImpl extends ServiceImpl<DishDao, Dish> implements DishService {
    @Autowired
    private DishFlavorService dishFlavorService;

    @Autowired
    private DishService dishService;

    /**
     * 新增菜品，同时保存对应的口味数据
     * @param dishDto
     */

    @Override
    public void saveWithFlavor(DishDto dishDto) {
        //保存菜品的基本信息到菜品表
        //只能在数据库存储有的字段，dish的字段
        this.save(dishDto);

        //菜品id
        Long dishId = dishDto.getId();
        //菜品口味
        List<DishFlavor> flavors = dishDto.getFlavors();

        flavors = flavors.stream().map(item -> {
            item.setDishId(dishId);
            return item;
        }).collect(Collectors.toList());

        //保存菜品口味数据到菜品口味表dish_flavor
        dishFlavorService.saveBatch(flavors);
    }

    /**
     * 根据id查询菜品信息和对应的口味信息
     * @param id
     * @return
     */
    @Override
    public DishDto getByIdWithFlavor(Long id) {
        //查询菜品基本信息，从dish表查询
        Dish dish = this.getById(id);

        //查询当前菜品对应的口味信息，从dish_flavor表查询
        LambdaQueryWrapper<DishFlavor> lqw = new LambdaQueryWrapper<>();
        lqw.eq(DishFlavor::getDishId,id);
        List<DishFlavor> flavors = dishFlavorService.list(lqw);

        // dish + dishFlavors = DishDto
        DishDto dishDto = new DishDto();

        //dish  => DishDto
        BeanUtils.copyProperties(dish,dishDto);

        //DishFlavors => DishDto
        dishDto.setFlavors(flavors);

        return dishDto;
    }

    /**
     * 修改菜品同时修改口味
     * @param dishDto
     */
    @Override
    public void updateWithFlavor(DishDto dishDto) {
        //1. 菜品的基本字段
        this.updateById(dishDto);
        //2. 修改菜品口味信息
        //  删除 + 新增 = 修改
        //2.1 删除这个原有的口味

        LambdaQueryWrapper<DishFlavor> lqw = new LambdaQueryWrapper<>();
        lqw.eq(DishFlavor::getDishId,dishDto.getId());
        dishFlavorService.remove(lqw);

        //2.2 新增新的口味
        List<DishFlavor> flavors = dishDto.getFlavors();

        //2.3 口味dishid赋值
        flavors = flavors.stream().map(item -> {
            item.setDishId(dishDto.getId());
            return item;
        }).collect(Collectors.toList());

        dishFlavorService.saveBatch(flavors);
    }

    /**
     * 删除菜品
     * @param ids
     */
    @Override
    public void deleteWithDish(List<Long> ids) {
        QueryWrapper<Dish> qw = new QueryWrapper<>();
        qw.lambda().in(Dish::getId,ids);
        qw.lambda().eq(Dish::getStatus,1);
        qw.select("count(id) as count");
        Map<String, Object> map = this.getMap(qw);
        Long count = (Long) map.get("count");
        if (count > 0){
            throw new CustomException("该菜品正在售卖，不能删除");
        }

        this.removeByIds(ids);

        LambdaQueryWrapper<DishFlavor> lqw = new LambdaQueryWrapper<>();
        lqw.in(DishFlavor::getDishId,ids);
        dishFlavorService.remove(lqw);
    }

    /**
     * 停售，起售
     * @param status
     * @param ids
     */
    @Override
    public void statusWithDish(Integer status, List<Long> ids) {
        LambdaQueryWrapper<Dish> lqw = new LambdaQueryWrapper<>();
        lqw.in(Dish::getId,ids);
        List<Dish> list = this.list(lqw);
        log.info("list集合是------{}",list);
        for (Dish dish : list) {
            dish.setStatus(status);
            dishService.updateById(dish);
        }
    }

}
